'use strict';

/** entrance of yqj-ms chat server **/

let config = require('config');
let express = require('express');
let app = express();

global._root_ = __dirname + '/';
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

/** setup routes **/
// app.get('/', (req, res, next) => {
//     res.render('app');
// });



app.use('/static', express.static(__dirname + '/dist/static'));

/** route api **/
app.use('/api', require('./api/index'));

// if(process.env.NODE_ENV === "production"){
  app.use(/^\/||^\/#.*/, function(req, res){
      res.sendfile(`${__dirname}/dist/index.html`)
  });
// }

/** catch 404 and forward to error handler **/
// app.use(function(req, res, next) {
//   var err = Error('Not Found');
//   err.status = 404;
//   next(err);
// });

// 404
app.get('*', function(req, res){
  res.send(404)
});

console.info(`this api port at ${config.ApiConfig.port}!`)
/** startup **/
app.listen(config.ApiConfig.port, (err) => {
    if (err) {
        console.error(err)
        return err;
    }
});
